﻿using CMPart;
using CSUtils;
using TripleJumpRoleCfg;
using U3dGame.Part;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    public class CSceneManage {
        public CCameraControl camControl;
        public TripleJump_FristRoleFly fristRole;

        public TripleJump_FristRoleFly[] roleArr;
        int[] posiO = new int[] { 0, -3, -2, -1, 1, 2, 3, 4 };

        public void Init() {
            Camera.main.backgroundColor = Color.red;
            camControl = CCameraControl.GetObj(null);

            TripleJump_FristRoleFly.CPara.onSetFlyState += camControl.SetFlyState;

            CDelayMain.Delay(0.3f).onCallback = delegate {
                _Init_Scene();
            };

            _SceneLoad();
            CCylinderTerrain.inst.Init();
            var firstPlayerCfg = CfgTJumpRole.firstPlayer;
            fristRole = GenerateRole(firstPlayerCfg, posiO[0]);
            TripleJump_FristRoleFly.CPara.firstRoleId = fristRole.id;
        }

        void _SceneLoad() {
            var terrain = GameObject.Find("terrain").transform;
            for (int i = 0; i < terrain.childCount; i++) {
                CCylinderTerrain.inst.AddToDeathList(terrain.GetChild(i));
            }
        }

        public void GameReset() {
            //Debug.LogErrorFormat("ddddddddddddddddddddddddddddddddddddd___________________dddddddddddddddddddd");
            if (roleArr != null) {
                for (int i = 1; i < roleArr.Length; i++) {
                    if (roleArr[i] != null) {
                        roleArr[i].Destroy();
                        //Part_021_BloodStrip bloodStrip = roleArray[i].obj.S_GetPart<Part_021_BloodStrip>();
                    }
                }
            }
            #if plus
            if (C_PlayerData.GetInstance.o_gameCount.Value > 1) {
                roleArray = new Part_10026_FristRoleFly[8];
            } else {
                roleArray = new Part_10026_FristRoleFly[1];
            }
            #else
            roleArr = new TripleJump_FristRoleFly[1];
            #endif
            roleArr[0] = fristRole;
            for (int i = 1; i < roleArr.Length; i++) {
                roleArr[i] = GenerateRole(CfgTJumpRole.firstPlayer, posiO[i]);
            }

            CCylinderTerrain.inst.Reset();

            camControl.GameReset();
            CDelayMain.Delay(0.1f).onCallback = delegate {
                for (int i = 0; i < roleArr.Length; i++) {
                    roleArr[i].GameReset();
                }
            };
        }

        public void GameStart() {
            CCylinderTerrain.inst.Open();
            camControl.GameStart();
            for (int i = 0; i < roleArr.Length; i++) {
                roleArr[i].GameStart();
            }
        }

        public void GameEnd() {
            CCylinderTerrain.inst.End();
            camControl.GameEnd();
            for (int i = 0; i < roleArr.Length; i++) {
                roleArr[i].GameEnd();
            }
        }

        void _Init_Scene() {
            // Camera.main.backgroundColor = Color.black;
            camControl.SetLight(CGameSetupTools.SetLight());
            //cameraControl.parameter.light.intensity = 1.5f;
            QualitySettings.shadowDistance = 20;
            Camera.main.clearFlags = CameraClearFlags.SolidColor;
            Camera.main.backgroundColor = Color.black.SetColor("8ccbff");
            RenderSettings.ambientLight = Color.white.SetColor("828282");
            //RenderSettings.ambientGroundColor = Color.white.S_SetColor("828282");

            RenderSettings.fog = true;
            RenderSettings.fogColor = Camera.main.backgroundColor;
            RenderSettings.fogMode = FogMode.Linear;
            RenderSettings.fogStartDistance = 200;
            RenderSettings.fogEndDistance = 600;
        }

        /// <summary>
        /// 生成角色
        /// </summary>
        TripleJump_FristRoleFly GenerateRole(CfgTJumpRole roleCfg, int posX) {
            var pos = new Vector3(posX, 0, TripleJump_FristRoleFly.CPara.roleStartPosZ);
            var role = CRoleManage.instance.AddNewRole<TripleJump_FristRoleFly>(roleCfg.roleInfo, pos);

            #if plus
            Part_021_BloodStrip bloodStrip = role.obj.S_GetPart<Part_021_BloodStrip>();
            bloodStrip.paramter.roleRoot = role.obj.a5root.transform.Find("cattle3/role/joint1");

            CDelayMain.Delayed(0.1f).d_EndEvent = delegate () {
                if (Part_10026_FristRoleFly.C_Parameter.o_fristRoleId == objId) {
                    role.parameter.playerNameBackColor = Color.yellow;
                    role.parameter.playerNameColor = Color.green;
                    role.parameter.playerName = C_PlayerData.GetInstance.o_playerName.Value;
                } else {
                    role.parameter.playerNameBackColor = C_Color.S_GetRandomColor();
                    role.parameter.playerNameColor = C_Color.S_GetRandomColor();
                    role.parameter.playerName = PlayerData.Cfg_AiPlayerInfo.S_GetRandomCfg().name;
                }
                bloodStrip.S_SetColor(role.parameter.playerNameBackColor);
                bloodStrip.S_SetName(role.parameter.playerName, role.parameter.playerNameColor);
            };
            #endif
            return role;
        }
    }
}
